<?php

namespace app\api\controller;


use app\common\exception\ApiException;
use app\common\service\UserToken;
use think\Cache;
use think\Controller;

class ApiBase extends Controller {
    public $user_id;
    public $openid;
    public $page = 1;
    public $size = 10;
    public $from = 0;

    protected $beforeActionList = [
        'checkUserToken' => ['except' => 'getToken,index,read,wxNotice']
    ];

    public function checkUserToken() {
        $tokenKey   = request()->header('token');
        $tokenValue = Cache::get($tokenKey);
        if (!$tokenValue) {
            throw new ApiException('token错误', 201, 10);
        }
        $this->user_id = UserToken::getCurrentTokenVar('user_id', $tokenValue);
        $this->openid  = UserToken::getCurrentTokenVar('openid', $tokenValue);
        UserToken::updateCache($tokenKey, $tokenValue);
    }

    /*获取分页page size 内容*/
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : 8;
        $this->from = ($this->page - 1) * $this->size;
    }
}